package com.xy.tool.util;

import java.sql.Timestamp;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

/**
 * @author Greekn Zhang
 * @date 2021/7/20 10:41
 * @description
 */
public class DateUtils {

    private static final ThreadLocal<ConcurrentHashMap<String, LocalDateTime>> TIME = new ThreadLocal<>();
    private static final String BEGIN_TIME = "beginDate";
    private static final String END_TIME = "endDate";

    public static String formatDateTime(long mss) {
        String DateTimes = null;
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = mss % 60;
        if (days > 0) {
            DateTimes = days + "天" + hours + "小时" + minutes + "分钟"
                    + seconds + "秒";
        } else if (hours > 0) {
            DateTimes = hours + "小时" + minutes + "分钟"
                    + seconds + "秒";
        } else if (minutes > 0) {
            DateTimes = minutes + "分钟"
                    + seconds + "秒";
        } else {
            DateTimes = seconds + "秒";
        }
        return DateTimes;
    }

    public static void getTimePeriod(Integer type) {
        LocalDateTime now = LocalDateTime.now();
        ConcurrentHashMap<String, LocalDateTime> time = new ConcurrentHashMap<>();
        //本周一
        LocalDateTime nowMonday = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(DayOfWeek.MONDAY);
        switch (type) {
            //本周(周一至现在)
            case 0:
                time.put(BEGIN_TIME, nowMonday);
                time.put(END_TIME, now);
                TIME.set(time);
                break;
            //上周
            case 1:
                //上周一
                LocalDateTime lastMonday = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).minusWeeks(1).with(DayOfWeek.MONDAY);
                time.put(BEGIN_TIME, lastMonday);
                time.put(END_TIME, LocalDateTime.of(nowMonday.toLocalDate().plusDays(-1), LocalTime.MAX));
                TIME.set(time);
                break;
            //本月
            case 2:
                LocalDateTime nowMonth = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
                time.put(BEGIN_TIME, nowMonth);
                time.put(END_TIME, now);
                TIME.set(time);
                break;
            //下周
            case 3:
                LocalDate today = LocalDate.now();
                LocalDate nextWeek = today.plusWeeks(1);
                time.put(BEGIN_TIME, LocalDateTime.of(today, LocalTime.MIN));
                time.put(END_TIME, LocalDateTime.of(nextWeek, LocalTime.MIN));
                TIME.set(time);
                break;
            //近三月
            case 4:
                today = LocalDate.now();
                LocalDate threeMonthsAgoBefore = today.plusMonths(-3);
                time.put(BEGIN_TIME, LocalDateTime.of(threeMonthsAgoBefore, LocalTime.MIN));
                time.put(END_TIME, LocalDateTime.of(today, LocalTime.MIN));
                TIME.set(time);
                break;
            //近半年
            case 5:
                today = LocalDate.now();
                LocalDate halfYearAgo = today.plusMonths(-6);
                time.put(BEGIN_TIME, LocalDateTime.of(halfYearAgo, LocalTime.MIN));
                time.put(END_TIME, LocalDateTime.of(today, LocalTime.MIN));
                TIME.set(time);
                break;
            //近一年
            case 6:
                today = LocalDate.now();
                LocalDate aYearAgo = today.plusYears(-1);
                time.put(BEGIN_TIME, LocalDateTime.of(aYearAgo, LocalTime.MIN));
                time.put(END_TIME, LocalDateTime.of(today, LocalTime.MIN));
                TIME.set(time);
                break;
            //本周（整周）
            case 7:
                //本周日
                LocalDateTime nowSunday = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(DayOfWeek.SUNDAY);
                time.put(BEGIN_TIME, nowMonday);
                time.put(END_TIME, nowSunday);
                TIME.set(time);
                break;
            //本月（整月）
            case 8:
                //本月一号
                LocalDateTime nowMonthFirstDate = LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
                //次月一号
                LocalDateTime nextMonthFirstDate = LocalDateTime.of(LocalDate.now().plusMonths(1).with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
                time.put(BEGIN_TIME, nowMonthFirstDate);
                time.put(END_TIME, nextMonthFirstDate);
                TIME.set(time);
                break;
            default:
                break;
        }

    }

    public static LocalDateTime getBeginTime() {
        ConcurrentHashMap<String, LocalDateTime> map = TIME.get();
        if (map.containsKey(BEGIN_TIME)) {
            return map.get(BEGIN_TIME);
        }
        return null;
    }

    public static LocalDateTime getEndTime() {
        ConcurrentHashMap<String, LocalDateTime> map = TIME.get();
        if (map.containsKey(END_TIME)) {
            return map.get(END_TIME);
        }
        return null;
    }

    public static List<String> getBetweenDate(LocalDate startDate, LocalDate endDate) {
        List<String> list = new ArrayList<>();
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        if (distance < 1) {
            return list;
        }
        Stream.iterate(startDate, d -> {
            return d.plusDays(1);
        }).limit(distance + 1).forEach(f -> {
            list.add(f.toString());
        });
        return list;
    }

    public static void remove() {
        TIME.remove();
    }
}
